﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Security;
using System.Net;
using System.IO;
using System.Security.Cryptography;

namespace SmartSiteManagementSystem.Utils
{
    public class EncryptionUtils
    {

        /// <summary>
        /// 得到中台登录用户的加密密码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <returns></returns>
        public static string GetAdminUserLoginPwd_SHA1(string userName, string userPwd)
        {
            return SHA1Encrypt("SMMS" + userName + userPwd, true, false);
        }

        /// <summary>
        /// 得到前台登录用户的加密密码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <returns></returns>
        public static string GetUserLoginPwd_SHA1(string userName, string userPwd)
        {
            return SHA1Encrypt("SMMSUser" + userName + userPwd, true, false);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <returns></returns>
        public static string GetUserLoginAuth_SHA1(string userName, string userPwd)
        {
            return SHA1Encrypt("SMMSUser" + userName + userPwd+"666888", true, false);
        }


        /// <summary> 
        /// SHA1加密字符串 
        /// </summary> 
        /// <param name="source">源字符串</param> 
        /// <param name="isReplace">是否替换掉加密后的字符串中的"-"字符</param>
        /// <param name="isToLower">是否把加密后的字符串转小写</param>
        /// <returns>加密后的字符串</returns> 
        public static string SHA1Encrypt(string source, bool isReplace = false, bool isToLower = false)
        {
            SHA1 sha1 = SHA1.Create();
            byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(source));
            string shaStr = BitConverter.ToString(hash);
            if (isReplace)
            {
                shaStr = shaStr.Replace("-", "");
            }
            if (isToLower)
            {
                shaStr = shaStr.ToLower();
            }
            return shaStr;
        }







        /// <summary> 
        /// MD5加密字符串 
        /// </summary> 
        /// <param name="source">源字符串</param> 
        /// <returns>加密后的字符串</returns> 
        public static string MD5(string source)
        {
            //return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5");
            return MD5Lower(source).ToUpper();
        }
        public static string MD5Lower(string source, string enc = "UTF-8")
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] t = md5.ComputeHash(Encoding.GetEncoding(enc).GetBytes(source));
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < t.Length; i++)
            {
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            }
            return sb.ToString();
        }


    }
}
